package com.lxc.C2DM;

import java.util.Map;
import java.util.TreeMap;

import com.lxc.C2DM.http.URLPost;

/**
 * 
 * @author Shineflag
 *
 */
public class GetAuth {
	

	/**中国象棋的auth*/
	public static final String chinesechess_auth = "DQAAAMQAAAD7bGO2Cc-LZIQLqIfk-A0XZdVUUc7r75ugbe-VSsBi8Ikl27VkierUnlSlqqLKMcRFix5hAuOGqcJdiU82ju3bsYG0nFCVWl7Ml4vLG2j9rIKvoFKTXc-l7C2oSxGHJ3fJL1fTdpjYrdbEvI0C-k_J-tf63X-7afYbfelhkXGIwffKA4CQbqFENwrBx-ge0PyS1PDFJo-XHsb3Ht-ejcRKM6zUG8Vw8wJdjHqp2XQxphOH-gVdASkLELyJn6gYElPWiVqDkJAJ2e5TtgZ8h12H";
	public static final String chinesechess_lt26i = "APA91bGHn0E88WOo_Aj87ix6pQ34E9dmu6hxj9wiSM7HxtzoC6QOAzl6DAFq_bdvemSti996Zq1QmE8Adw6A-iiRxPBGa6e-hRnE4rlAyoTXC--XLpoC1DxZdED2kWwGOB_wOFeX7Xkze3wxjdmYEDsSBdw3GV3XQw";
	/**
	 * @param args
	 */
	public static void main(String[] args) {
//		String rid = "APA91bFwyPhJ6idmevXUWE6sa_S16G4KbbK5JHv5nub3Hla95ND7sJ62DQQSZCjOCpEt3OvRUNCwu7KfqeoHCBzd859mf3qiIHa3cXB6MFM7KT0RUGqAhum2L4PlQ79_jrEj2gmM3-sX";
		String auth = chinesechess_auth;//getAUTH();
		if(auth == null){
			System.out.println("get auth fail!!");
			return;
		}
		
		System.out.println("auth = "+auth);
		Map<String,String> message = new TreeMap<String,String>();
		message.put("registration_id", chinesechess_lt26i);  //识别手机的ID
		message.put("collapse_key", "1");     //消息的类型，同一类型消息只接收最后一条
		message.put("data.account_name", "shineflag");  //消息以键值对形式出现，可有多个
		message.put("data.message", "来决战到天亮");
		
		postMessage(auth, message);
	}
	
	/**
	 * 获取验证头，每次获取都是不同的，但我们只要任一个就行了
	 * @return  验证头(失败为空)
	 */
	public static String getAUTH(){
		String url = "https://www.google.com/accounts/ClientLogin";//获取Auth的URL
		Map<String,String> params = new TreeMap<String,String>();
		params.put("accountType","HOSTED_OR_GOOGLE");
		params.put("Email","boyaachinesechess@gmail.com");
		params.put("Passwd","boyaachinesechess123");
		params.put("service","ac2dm");
		params.put("source","boyaa-chinesechess-1.0");
		String result = URLPost.PostURL(url, params,20*1000);
		String [] results = result.split("=");
		if(results == null || results.length <4)//获取认证失败
			return null;
		return results[3].trim();

	}
	
	/**
	 * 发送推送消息
	 * @param auth
	 * @param message
	 * @return
	 */
	public static String postMessage(String auth,Map<String,String> message){
		String url = "https://android.apis.google.com/c2dm/send";//发送推送消息的URL(固定的)
		Map<String,String> header = new TreeMap<String,String>();
		header.put("Authorization","GoogleLogin auth="+auth);    //推送消息的证证头
		String result = URLPost.PostURL(url, message, header, 20*1000);
		return result;
		
	}

}
